package demo.gl;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.view.MotionEvent;

public class vienbi {
	private int SoDong=10;
	private int SoCot=10;
	private float sizeOCo=0.5f;	//size phải giống với size trong class banco
	private float BanKinh=0.2f;
	private float DoDayBC=0.2f;
	
	private int mauDo=1;
	private  int mauVang=2;
	private  int mauXanhLa=3;
	private  int mauXanhDT=4;
	private  int mauHong=5;
	private  int mauXanhLam=6;
	private int mau=mauHong;
	
	private int TongSoDinh=(SoCot+1)*2*SoDong;
	private float ToaDoBi_TaiTamO[] = new float[ (TongSoDinh*3)]; 
	
	

	private FloatBuffer phanThan_Buffer;
	
	private FloatBuffer colorThan_Buffer;
	public float[] getToaDo(){
		return ToaDoBi_TaiTamO;
	}
	private int XMaTran;
	private int YMaTran;
	public int getXMaTran(){
		return XMaTran;
	}
	public int getYMaTran(){
		return YMaTran;
	}
	public vienbi(float bankinh,int mausac,int x,int y,int sogach){
		//for(int i=0;i<ToaDoBi_TaiTamO.length;i+=1)
		//	ToaDoBi_TaiTamO[i]=2+ToaDoBi_TaiTamO[i];
		BanKinh=bankinh;
		mau=mausac;
		TaoToaDoBi_TaiTamO();

		mau=mausac;
		for(int i=0;i<ToaDoBi_TaiTamO.length;i+=3){
				ToaDoBi_TaiTamO[i]=((x-(int)(sogach/2))*sizeOCo+ToaDoBi_TaiTamO[i]);
				
		}
		for(int i=1;i<ToaDoBi_TaiTamO.length;i+=3){
				ToaDoBi_TaiTamO[i]=(((int)(sogach/2)-y)*sizeOCo+ToaDoBi_TaiTamO[i]);
		}
		XMaTran=x;
		YMaTran=y;
		KhoiTao();
	}
	/**
	 * 
	 * @param bankinh
	 * @param mausac	mausac sẽ đi từ 1 đến 6 , tương ứng cho Đỏ, Vàng, Xanh lá, Xanh da trời, Hồng, Xanh lam
	 */
	public vienbi(float bankinh,int mausac){
		BanKinh=bankinh;
		mau=mausac;
		TaoToaDoBi_TaiTamO();

		KhoiTao();
			
	}
	public vienbi(){
		//TaoToaDoBi_TaiTamO();

		KhoiTao();
		
	}
	public void KhoiTao(){
		
		//TaoColorCuc();
		TaoColorThan();
		
	
		
		ByteBuffer vbb2=ByteBuffer.allocateDirect(ToaDoBi_TaiTamO.length*4);
		vbb2.order(ByteOrder.nativeOrder());
		phanThan_Buffer=vbb2.asFloatBuffer();
		phanThan_Buffer.put(ToaDoBi_TaiTamO);
		phanThan_Buffer.position(0);
		
	
	
		
		ByteBuffer vbb5=ByteBuffer.allocateDirect(colorPhanThan.length*4);
		vbb5.order(ByteOrder.nativeOrder());
		colorThan_Buffer=vbb5.asFloatBuffer();
		colorThan_Buffer.put(colorPhanThan);
		colorThan_Buffer.position(0);
	}
	

	
	
	public void TaoToaDoBi_TaiTamO(){
	
		float TheTa=(float) (Math.PI/SoDong);
		float Phi=(float) (2.0f*Math.PI/SoCot);
		float Phi_tam=0.0f;
		int i=0;
		for(int j=0;j<SoDong;j++){
			float TheTa_tam=j*TheTa;
			for(int k=0;k<=SoCot;k++){
				
				if(k==SoCot)
					Phi_tam=0.0f;
				else
					Phi_tam=k*Phi;
					
				
				//Tao toa do đỉnh trên
				ToaDoBi_TaiTamO[i]=(float) (BanKinh*Math.cos(Phi_tam)*Math.sin(TheTa_tam));	
				i++;
				ToaDoBi_TaiTamO[i]=(float) (BanKinh*Math.sin(Phi_tam)*Math.sin(TheTa_tam));	
				i++;
				ToaDoBi_TaiTamO[i]=(float) (BanKinh*Math.cos((TheTa_tam)));	
				i++;
				//Tạo tọa độ định dưới. 1 cái trên, 1 cái dưới, liên tục như vậy sẽ vẽ được nhiều tam giác, gép lại thành mặt cầu.
				ToaDoBi_TaiTamO[i]=(float) (BanKinh*Math.cos(Phi_tam)*Math.sin(TheTa_tam+TheTa));	
				i++;
				ToaDoBi_TaiTamO[i]=(float) (BanKinh*Math.sin(Phi_tam)*Math.sin(TheTa_tam+TheTa));	
				i++;
				ToaDoBi_TaiTamO[i]=(float) (BanKinh*Math.cos((TheTa_tam+TheTa)));	
				i++;
				
			}
		}
		for(int t=0;t<ToaDoBi_TaiTamO.length;t++){
			if((t-2)%3==0)
				ToaDoBi_TaiTamO[t]+=BanKinh+DoDayBC;
		}
	
	}

	

	
	private float[] colorPhanThan= new float[TongSoDinh*4];

	public void TaoColorThan(){
		float phamau=0.9f;
		float phatrang=1.0f;
		if(mau==mauDo){
			for(int i=0;i<TongSoDinh*4;i++){
				if(i%4==0)
					colorPhanThan[i]=phamau;
				else if((i-3)%4==0)
					colorPhanThan[i]=1.0f;
				else
					if(i>10)
						colorPhanThan[i]=phatrang;
				phamau-=0.001f;
				phatrang-=0.01;
			}
		}
		else if(mau == mauXanhLa){
			for(int i=0;i<TongSoDinh*4;i++){
				if((i-1)%4==0)
					colorPhanThan[i]=phamau;
				else if((i-3)%4==0)
					colorPhanThan[i]=1.0f;
				else
					if(i>10)
						colorPhanThan[i]=phatrang;
				phamau-=0.001f;
				phatrang-=0.01;
			}
		}
		else if(mau==mauXanhLam){
			for(int i=0;i<TongSoDinh*4;i++){
				if((i-2)%4==0)
					colorPhanThan[i]=phamau;
				else if((i-3)%4==0)
					colorPhanThan[i]=1.0f;
				else
					if(i>10)
						colorPhanThan[i]=phatrang;
				phamau-=0.001f;
				phatrang-=0.01;
			}
		}
		else if(mau==mauVang){
			phamau=0.8f;
			for(int i=0;i<TongSoDinh*4;i++){
				
				
				if(i%4==0 || (i-1)%4==0)
					colorPhanThan[i]=phamau;
				else if((i-3)%4==0){
					colorPhanThan[i]=1.0f;
					phamau-=0.001f;
				}
				else
					if(i>10)
						colorPhanThan[i]=phatrang;
			
				phatrang-=0.01;
					
			}
		}
		else if(mau==mauXanhDT){
			phamau=0.8f;
			for(int i=0;i<TongSoDinh*4;i++){			
				if((i-1)%4==0 || (i-2)%4==0)
					colorPhanThan[i]=phamau;
				else if((i-3)%4==0){
					colorPhanThan[i]=1.0f;
					phamau-=0.001f;
				}
				else
					if(i>10)
						colorPhanThan[i]=phatrang;
				
				phatrang-=0.01;					
			}
		}
		else if(mau==mauHong){
			phamau=0.85f;
			for(int i=0;i<TongSoDinh*4;i++){
				if(i%4==0 || (i-2)%4==0)
					colorPhanThan[i]=phamau;
				else if((i-3)%4==0){
					colorPhanThan[i]=1.0f;
					phamau-=0.001f;
				}
				else
					if(i>10)
						colorPhanThan[i]=phatrang;
			
				phatrang-=0.01;
			}
		}
	}
	public void Draw(GL10 gl){
		//gl.glFrontFace(GL10.GL_CW);
		

		
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, phanThan_Buffer);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0,colorThan_Buffer );
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		
	
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, TongSoDinh);
		//gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		//gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

	}

}
